package com.jwsoft.manager.core.gateway.controller;

import com.alibaba.fastjson.JSON;
import com.bifang.assistant.common.constants.Validators;
import com.bifang.assistant.common.vo.BaseVO;
import com.bifang.engine.assistant.core.domain.AppResponse;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.protect.core.annotation.Idempotent;
import com.bifang.engine.protect.core.annotation.NoSignCrypto;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.common.vo.excel.ExcelReadVO;
import com.bifang.module.base.common.vo.excel.ExcelVO;
import com.bifang.module.base.core.utils.ExcelFileUtil;
import com.bifang.module.base.core.utils.ServiceRequestUtil;
import com.bifang.module.system.core.integration.impl.ExcelIntegrationImpl;
import com.bifang.module.system.core.integration.impl.UserIntegrationImpl;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.vo.common.AreaControlVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPre.*;
import com.jwsoft.manager.common.vo.student.StudentInfoSearchVO;
import com.jwsoft.manager.common.vo.student.StudentInfoSelectVO;
import com.jwsoft.manager.core.dao.service.EduSchoolService;
import com.jwsoft.manager.core.integration.EduStudentEnrollPreIntegration;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 预采集表网关
 *
 * @author wangtao
 * @since 2022-11-25
 */
@RestController
@RequestMapping(value = "/doc/api")
@Tag(name = "预采集管理", description = "预采集管理")
public class EduStudentEnrollPreController {

    @Autowired
    public EduStudentEnrollPreIntegration eduStudentEnrollPreIntegration;

    @Resource
    public ExcelIntegrationImpl excelIntegration;

    @Autowired
    public UserIntegrationImpl userIntegration;

    @Autowired

    public EduSchoolService eduSchoolService;

    @Operation(summary = "预采集表分页查询",description = "预采集表分页查询")
    @PostMapping(value = "/edustudentenrollpre0001", produces = "application/json;charset=UTF-8")
    public AppResponse<PageInfo<EduStudentEnrollPreVO>> getList(@RequestBody @Validated EduStudentEnrollPreQueryVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getList(vo));
    }

    @Operation(summary = "预采集表根据主键查询详情",description = "预采集表根据主键查询详情")
    @PostMapping(value = "/edustudentenrollpre0002", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> getById(@RequestBody @Validated EduStudentEnrollPreKeyVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getById(vo));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "预采集表新增-excel导入保存",description = "预采集表新增-excel导入保存")
    @PostMapping(value = "/edustudentenrollpre0004", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> add(@RequestBody  EduStudentEnrollExcelSaveVO vo) {
        eduStudentEnrollPreIntegration.addExcel(vo);
        return AppResponse.successMsg("保存成功！");
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "预采集表修改",description = "预采集表修改")
    @PostMapping(value = "/edustudentenrollpre0005", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> edit(@RequestBody @Validated(value = {Validators.Edit.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.edit(vo));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "预采集表删除",description = "预采集表删除")
    @PostMapping(value = "/edustudentenrollpre0006", produces = "application/json;charset=UTF-8")
    public AppResponse del(@RequestBody @Validated EduStudentEnrollPreKeyVO vo) {
        eduStudentEnrollPreIntegration.del(vo);
        return AppResponse.successMsg("删除成功！");
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "预采集表文件上传（PC）",description = "预采集表文件上传")
    @PostMapping(value = "/edustudentenrollpre0007", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public AppResponse<ExcelReadVO> readByCode(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            HttpServletResponse response) throws IOException {
        ExcelVO vo= ExcelFileUtil.getExcelVO(request,response,paramMap);
        MultipartFile file=ExcelFileUtil.getExcelFile(request);
        return AppResponse.successData(eduStudentEnrollPreIntegration.readExcel(vo, file));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "预采集模板下载（PC）",description = "预采集模板下载")
    @PostMapping(value = "/edustudentenrollpre0008", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public void downloadByCode(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            @RequestBody(required = false) String body,
            HttpServletResponse response)  {
        ExcelVO vo = null;
        if (StringUtils.isNotBlank(body)) {
            vo = JSON.parseObject(body, ExcelVO.class);
        } else {
            vo = new ExcelVO();
            if (paramMap.get("modelCode") != null) {
                vo.setModelCode(paramMap.get("modelCode").toString());
            }
            if (paramMap.get("excelVersion") != null) {
                vo.setExcelVersion(paramMap.get("excelVersion").toString());
            }
            if (paramMap.get("rows") != null) {
                vo.setRows(Integer.parseInt(paramMap.get("rows").toString()));
            }
            if (paramMap.get("dictMap") != null) {
                vo.setDictMap(
                        (Map<String, List<SelectVO>>)
                                JSON.parse(JSON.toJSONString(paramMap.get("dictMap"))));
            }

        }
        vo.setAccessToken(ServiceRequestUtil.getAccessToken(request, response));
        if (StringUtils.isBlank(vo.getModelCode())) {
            throw new AppException("excel模板编号不能为空");
        }

        eduStudentEnrollPreIntegration.downloadModel(vo, response);
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "预采集管理学生新增：单个",description = "预采集管理学生新增：单个")
    @PostMapping(value = "/edustudentenrollpre0009", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> add(@RequestBody @Validated(value = {Validators.Add.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.add(vo));
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "家长端准新生登记保存",description = "家长端准新生登记保存")
    @PostMapping(value = "/edustudentenrollpre0010", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> savePre(@RequestBody @Validated(value = {Validators.Add.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.savePre(vo));
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "家长端准新生登记保存金华",description = "家长端准新生登记保存金华")
    @PostMapping(value = "/edustudentenrollpreJh0010", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> savePreJh(@RequestBody @Validated(value = {Validators.Add.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.savePreJh(vo));
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "家长端准新生修改金华",description = "家长端准新生修改金华")
    @PostMapping(value = "/edustudentenrollpreJh0011", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> editPreJh(@RequestBody @Validated(value = {Validators.Edit.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.editPreJh(vo));
    }
    @Idempotent(expire = "PT2S") //2秒内防重复提交
    @Operation(summary = "准新生扫码登记", description = "准新生扫码登记")
    @PostMapping(value = "/edustudentenrollpre0017", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> scanningSavePre(@RequestBody @Validated(value = {Validators.Add.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.scanningSavePre(vo));
    }

    @Idempotent(expire = "PT2S") //2秒内防重复提交
    @Operation(summary = "根据身份证查询本年度学生登记信息是否存在", description = "根据身份证查询本年度学生登记信息是否存在")
    @PostMapping(value = "/edustudentenrollpre0018", produces = "application/json;charset=UTF-8")
    public AppResponse<String> checkByIdcard(@RequestBody CheckByIdcardVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.checkByIdcard(vo));
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "家长端准新生修改",description = "家长端准新生修改")
    @PostMapping(value = "/edustudentenrollpre0011", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> editPre(@RequestBody @Validated(value = {Validators.Edit.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.editPre(vo));
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "预采集管理新生信息审核",description = "预采集管理新生信息审核")
    @PostMapping(value = "/edustudentenrollpre0012", produces = "application/json;charset=UTF-8")
    public AppResponse auditEnrollPre(@RequestBody @Validated EnrollPreAuditVO vo) {
        eduStudentEnrollPreIntegration.auditEnrollPre(vo);
        return AppResponse.successMsg("审核成功！");
    }
    @Operation(summary = "学生搜索",description = "学生搜索")
    @PostMapping(value = "/studentInfo0001", produces = "application/json;charset=UTF-8")
    public AppResponse<PageInfo<StudentInfoSelectVO>> searchStudentSelect(@RequestBody @Validated StudentInfoSearchVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.searchStudentSelect(vo));
    }

    @RequestMapping(
            value = "/edustudentenrollpre0013",
            method = {RequestMethod.POST, RequestMethod.GET})
    @Operation(summary = "预采集错误数据下载", description = "预采集错误数据下载")
    @NoSignCrypto
    public void downloadErrorExcel(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            @RequestBody EnrollPreFileVO vo,
            HttpServletResponse response) {

        if (StringUtils.isBlank(vo.getModelCode())) {
            throw new AppException("excel模板编号不能为空");
        }
        eduStudentEnrollPreIntegration.downloadErrorExcel(vo,response);

    }

    @RequestMapping(
            value = "/edustudentenrollpre0014",
            method = {RequestMethod.POST, RequestMethod.GET})
    @Operation(summary = "预采集信息导出", description = "预采集信息导出")
    @NoSignCrypto
    public void downloadData(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            @RequestBody(required = false) EduStudentEnrollPreQueryVO vo,
            HttpServletResponse response) {

        if (StringUtils.isBlank(vo.getModelCode())) {
            throw new AppException("excel模板编号不能为空");
        }
        eduStudentEnrollPreIntegration.downloadData(vo,response);

    }
    @Operation(summary = "家长端当年适龄子女查询",description = "家长端当年适龄子女查询")
    @PostMapping(value = "/edustudentenrollpre0015", produces = "application/json;charset=UTF-8")
    public AppResponse<List<EduStudentEnrollPreVO>> getMyChilds(@RequestBody @Validated AreaControlVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getMyChilds(vo));
    }
    @Operation(summary = "金华家长端当年适龄子女查询",description = "金华家长端当年适龄子女查询")
    @PostMapping(value = "/edustudentenrollpreJh0015", produces = "application/json;charset=UTF-8")
    public AppResponse<List<EduStudentEnrollPreVO>> getMyChildsJh(@RequestBody @Validated AreaControlVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getMyChildsJh(vo));
    }
    @Operation(summary = "家长端当年适龄子女控制信息获取",description = "家长端当年适龄子女控制信息获取")
    @PostMapping(value = "/edustudentenrollpre0016", produces = "application/json;charset=UTF-8")
    public AppResponse<MyChildCheckVO> getMyChildCheckVO(@RequestBody @Validated AreaControlVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getMyChildCheckVO(vo));
    }

    @Operation(summary = "根据学生idCard查询预采集信息",description = "根据学生idCard查询预采集信息")
    @PostMapping(value = "/edustudentenrollpre0021", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> getStudentEnrollPreByIdCard(@RequestBody @Validated EduStudentEnrollPreQueryVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getStudentEnrollPreByIdCard(vo));
    }

    @Operation(summary = "校验学生年龄是否符合",description = "校验学生年龄是否符合")
    @PostMapping(value = "/edustudentenrollpre0022", produces = "application/json;charset=UTF-8")
    public AppResponse<Boolean> checkAge(@RequestBody @Validated CheckAgeVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.checkAge(vo));
    }
    @Operation(summary = "",description = "单个一次准新生登记或修改")
    @PostMapping(value = "/edustudentenrollpre0023", produces = "application/json;charset=UTF-8")
    public AppResponse unLockOne(@RequestBody @Validated EduStudentEnrollPreKeyVO vo) {
        eduStudentEnrollPreIntegration.unLock(vo);
        return AppResponse.successMsg("操作成功");
    }
    @Operation(summary = "",description = "批量解锁一次")
    @PostMapping(value = "/edustudentenrollpre0024", produces = "application/json;charset=UTF-8")
    public AppResponse unLock(@RequestBody @Validated AreaControlVO vo) {
        eduStudentEnrollPreIntegration.unLock(vo);
        return AppResponse.successMsg("操作成功");
    }

    @Operation(summary = "获取出生证比对状态异常的数据",description = "获取出生证比对状态异常的数据")
    @PostMapping(value = "/edustudentenrollpre0025", produces = "application/json;charset=UTF-8")
    public AppResponse<PageInfo<EduStudentEnrollPreVO>> getFamilyFlagList(@RequestBody @Validated EduStudentEnrollPreQueryVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getFamilyFlagList(vo));
    }

    @Operation(summary = "",description = "修改家庭关系比对状态")
    @PostMapping(value = "/edustudentenrollpre0026", produces = "application/json;charset=UTF-8")
    public AppResponse updateFamilyFlag(@RequestBody @Validated EduStudentEnrollPreQueryVO vo) {
        eduStudentEnrollPreIntegration.updateFamilyFlag(vo);
        return AppResponse.successMsg("操作成功");
    }

    @Operation(summary = "",description = "家长端我的申请记录")
    @PostMapping(value = "/edustudentenrollpre0027", produces = "application/json;charset=UTF-8")
    public AppResponse<List<MyApplyVO>> getMyApplyList(@RequestBody @Validated BaseVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.getMyApplyList(vo));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "家长端学生信息登记保存（业务表）",description = "家长端学生信息登记保存（业务表）")
    @PostMapping(value = "/edustudentenrollpre0028", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> saveStudentInfoByAPP(@RequestBody @Validated(value = {Validators.Add.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.saveStudentInfoByAPP(vo));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "管理员端学生信息登记保存（结果表）",description = "管理员端学生信息登记保存（结果表）")
    @PostMapping(value = "/edustudentenrollpre0029", produces = "application/json;charset=UTF-8")
    public AppResponse<EduStudentEnrollPreVO> saveStudentInfoByAdmin(@RequestBody @Validated(value = {Validators.Add.class}) EduStudentEnrollPreVO vo) {
        return AppResponse.successData(eduStudentEnrollPreIntegration.saveStudentInfoByAdmin(vo));
    }
    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "管理员端重新全量获取大数据",description = "管理员端重新全量获取大数据")
    @PostMapping(value = "/edustudentenrollpre0030", produces = "application/json;charset=UTF-8")
    public AppResponse resetBigData(@RequestBody ResetBigDataVO vo) {
        eduStudentEnrollPreIntegration.resetBigData(vo);
        return AppResponse.successMsg("成功");
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "管理员端重置预报名状态",description = "管理员端重置预报名状态")
    @PostMapping(value = "/edustudentenrollpre0031", produces = "application/json;charset=UTF-8")
    public AppResponse resetYbm(@RequestBody ResetBigDataVO vo) {
        eduStudentEnrollPreIntegration.resetYbm(vo);
        return AppResponse.successMsg("成功");
    }
}
